﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

using TileMapEditor.Tools;

namespace TileMapEditor.Config
{
    [CustomEditor(typeof(TileMapSettings))]
    public class TileMapSettingsInspector : Editor
    {
        private TileMapSettings _target;

        public static void ShowWindow()
        {
            Selection.activeObject = ScriptableObject.CreateInstance<TileMapSettings>();
        }

        public void OnEnable()
        {
            _target = target as TileMapSettings;
            //Debug.Log( "TileMapSettingsInspector OnEnable" );
        }

        public void OnDisable()
        {
            //Debug.Log( "TileMapSettingsInspector OnDisable" );

            EditorPrefsWrapper.SetColor( TileMapSettings.TileMapSettingPrefsKeys.GridLineColor,
                TileMapSettings.GridLineColor );

            EditorPrefsWrapper.SetColor( TileMapSettings.TileMapSettingPrefsKeys.OverlayColor,
                TileMapSettings.OverlayColor );
        }

        public override void OnInspectorGUI()
        {
            EditorGUILayout.LabelField( TileMapSettingStyles.BasicSettings, EditorStyles.boldLabel );

            TileMapSettings.GridLineColor = EditorGUILayout.ColorField( TileMapSettingStyles.GridLineColorLabel,
                TileMapSettings.GridLineColor );

            TileMapSettings.OverlayColor = EditorGUILayout.ColorField( TileMapSettingStyles.OverlayColorLabel,
                TileMapSettings.OverlayColor );

            EditorGUILayout.BeginVertical();

            EditorGUILayout.LabelField( TileMapSettingStyles.PreviewColorLabel, EditorStyles.boldLabel );

            foreach ( var areaColor in TileMapSettings.areaColors )
            {
                EditorGUILayout.BeginHorizontal();

                EditorGUILayout.LabelField( new GUIContent( areaColor.Key.ToString() ) );
                EditorGUILayout.ColorField( areaColor.Value );

                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.EndVertical();

            if (GUI.changed)
            {
                serializedObject.ApplyModifiedProperties();
            }
        }

        internal static class TileMapSettingStyles
        {
            public static GUIContent BasicSettings = new GUIContent( "Basic Settings" );
            public static GUIContent GridLineColorLabel = new GUIContent( "Grid Line Color" );
            public static GUIContent OverlayColorLabel = new GUIContent( "Overlay Color" );
            public static GUIContent PreviewColorLabel = new GUIContent( "Preview Color" );
        }
    }

}
